package com.weilive.core.utils.ini;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Ini
{
    boolean load;
    String fileName;
    IniData iniData = new IniData();
    private InputStream ins;
    
    public Ini()
    {
        this.load = false;
        this.fileName = "";
    }
    
    public Ini(String fileName)
    {
        this.load = false;
        this.fileName = fileName;
    }
    
    public boolean testLoad()
    {
        return this.load;
    }
    
    public boolean loadIniFile()
    {
        if (this.fileName.length() <= 0)
            return false;
        try
        {
            ins = new FileInputStream(this.fileName);
            if (ins != null)
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        ins, "UTF-8"));
                
                String line = "";
                while ((line = br.readLine()) != null)
                {
                    if (line.indexOf(" ") != -1)
                    {
                        line = line.trim();
                    }
                    if ((line.length() < 2)
                            || ((line.charAt(0) == '/') && (line.charAt(1) == '/')))
                    {
                        continue;
                    }
                    if ((line.charAt(0) == '[')
                            && (line.charAt(line.length() - 1) == ']'))
                    {
                        line = line.substring(1, line.length() - 1);
                        if (this.iniData.getCount() > 0)
                        {
                            for (int j = 0; (j < this.iniData.getCount())
                                    && (!this.iniData.getSectionName(j).equals(
                                            line)); j++)
                                ;
                        }
                        
                        this.iniData.addSection(line);
                        continue;
                    }
                    
                    int charindex = line.indexOf("=");
                    if (charindex == -1)
                    {
                        continue;
                    }
                    String key = line.substring(0, charindex);
                    String value = line.substring(charindex + 1);
                    this.iniData.addKeyValue(this.iniData.getCount() - 1, key,
                            value);
                }
                br.close();
                return true;
            }
        }
        catch (IOException e)
        {
            System.out.println(e);
            return false;
        }
        return false;
    }
    
    public int findSection(String section)
    {
        for (int i = 0; i < this.iniData.getCount(); i++)
        {
            if (this.iniData.getSectionName(i).equals(section))
            {
                return i;
            }
        }
        return -1;
    }
    
    public int findKey(String section, String key)
    {
        int title = findSection(section);
        if (title == -1)
        {
            return -1;
        }
        
        return this.iniData.findKey(title, key);
    }
    
    public int findKey(int section, String key)
    {
        if ((section < 0) || (section >= this.iniData.getCount()))
        {
            return -1;
        }
        
        return this.iniData.findKey(section, key);
    }
    
    public String getKeyValue(String section, String key, String def)
    {
        int title = findSection(section);
        if (title == -1)
        {
            return def;
        }
        
        int keyindex = this.iniData.findKey(title, key);
        if (keyindex == -1)
        {
            return def;
        }
        
        return this.iniData.getKeyValue(title, keyindex);
    }
    
    public String getKeyValue(int section, String key, String def)
    {
        if ((section < 0) || (section >= this.iniData.getCount()))
        {
            return def;
        }
        
        int keyindex = this.iniData.findKey(section, key);
        if (keyindex == -1)
        {
            return def;
        }
        
        return this.iniData.getKeyValue(section, keyindex);
    }
    
    public String getKeyValue(int section, int keyindex, String def)
    {
        if ((section < 0) || (section >= this.iniData.getCount()))
        {
            return def;
        }
        
        if ((keyindex < 0) || (keyindex >= this.iniData.getKeyCount(section)))
        {
            return def;
        }
        
        return this.iniData.getKeyValue(section, keyindex);
    }
    
    public int getSectionCount()
    {
        return this.iniData.getCount();
    }
    
    public String getSectionName(int index)
    {
        return this.iniData.getSectionName(index);
    }
    
    public int getKeyCount(String section)
    {
        int title = findSection(section);
        if (title == -1)
        {
            return 0;
        }
        
        return this.iniData.getKeyCount(title);
    }
    
    public int getKeyCount(int index)
    {
        if ((index < 0) || (index >= this.iniData.getCount()))
        {
            return 0;
        }
        
        return this.iniData.getKeyCount(index);
    }
    
    public String getIndexKeyName(String section, int title, String def)
    {
        int index = findSection(section);
        if (index == -1)
        {
            return def;
        }
        
        return this.iniData.getKeyName(index, title);
    }
    
    public String getIndexKeyName(int section, int title, String def)
    {
        if ((section < 0) || (section >= this.iniData.getCount()))
        {
            return def;
        }
        
        if ((title < 0) || (title >= this.iniData.getKeyCount(section)))
        {
            return def;
        }
        
        return this.iniData.getKeyName(section, title);
    }
    
    public String getIndexKeyValue(String section, int key, String def)
    {
        int index = findSection(section);
        if (index == -1)
        {
            return def;
        }
        
        if ((key < 0) || (key >= this.iniData.getKeyCount(key)))
        {
            return def;
        }
        
        return this.iniData.getKeyValue(index, key);
    }
    
    public String getIndexKeyValue(int section, int key, String def)
    {
        if ((section < 0) || (section >= this.iniData.getCount()))
        {
            return def;
        }
        
        if ((key < 0) || (key >= this.iniData.getKeyCount(section)))
        {
            return def;
        }
        
        return this.iniData.getKeyValue(section, key);
    }
    
    public boolean addSection(String section)
    {
        if (findSection(section) == -1)
        {
            this.iniData.addSection(section);
            return true;
        }
        
        return false;
    }
    
    public boolean addKeyValue(String section, String key, String value)
    {
        int index = findSection(section);
        if ((index == -1) && (!addSection(section)))
        {
            return false;
        }
        
        return this.iniData.addKeyValue(index, key, value);
    }
    
    public boolean addKeyValue(int section, String key, String value)
    {
        if ((section < 0) || (section > this.iniData.getCount()))
        {
            return false;
        }
        
        return this.iniData.addKeyValue(section, key, value);
    }
    
    public boolean setSectionName(String section, String sectionname)
    {
        int index = findSection(section);
        if (index == -1)
        {
            return false;
        }
        
        return this.iniData.setSectionName(index, sectionname);
    }
    
    public boolean setKeyValue(String section, int key, String value)
    {
        int index = findSection(section);
        if (index == -1)
        {
            return false;
        }
        
        return this.iniData.setKeyValue(index, key, value);
    }
    
    public boolean setKeyValue(int section, int key, String value)
    {
        if ((section < 0) || (section >= this.iniData.getCount()))
        {
            return false;
        }
        
        return this.iniData.setKeyValue(section, key, value);
    }
    
    public boolean setKeyName(String section, String key, String keyname)
    {
        int index = findSection(section);
        if (index == -1)
        {
            return false;
        }
        
        int keyindex = this.iniData.findKey(index, key);
        if (keyindex == -1)
        {
            return false;
        }
        
        return this.iniData.setKeyName(index, keyindex, keyname);
    }
    
    public boolean setKeyName(String section, int key, String keyname)
    {
        int index = findSection(section);
        if (index == -1)
        {
            return false;
        }
        
        return this.iniData.setKeyName(index, key, keyname);
    }
    
    public boolean delSection(String section)
    {
        int index = findSection(section);
        if (index == -1)
        {
            return false;
        }
        
        return this.iniData.delSection(index);
    }
    
    public boolean delKey(String section, String key)
    {
        int index = findSection(section);
        if (index == -1)
        {
            return false;
        }
        
        int keyindex = this.iniData.findKey(index, key);
        if (keyindex == -1)
        {
            return false;
        }
        
        return this.iniData.delKey(index, keyindex);
    }
    
    public boolean delKey(String section, int key)
    {
        int index = findSection(section);
        if (index == -1)
        {
            return false;
        }
        
        return this.iniData.delKey(index, key);
    }
    
    public boolean annexFile(Ini other)
    {
        if (other.getSectionCount() < 0)
        {
            return false;
        }
        
        for (int i = 0; i < other.getSectionCount(); i++)
        {
            for (int j = 0; j < other.getKeyCount(i); j++)
            {
                String section = other.getSectionName(i);
                String key = other.getIndexKeyName(i, j, "");
                String value = other.getKeyValue(i, j, "");
                
                addKeyValue(section, key, value);
            }
        }
        return true;
    }
    
    public String toString()
    {
        return this.fileName;
    }
}